<template>
  <div>
    <el-container>
      <el-main>
        <kendoForm
          :kendo-form-items="kendoFormItems"
          :kendo-form="kendoForm"
          :kendo-params-value="kendoParamsValue"
        />
      </el-main>
    </el-container>
    <el-container style="padding-left:5px">
      <el-main>
        <KendoGridCmpt
          :ref="'PrjMemberAtdSeltKendoGrid'"
          :table-grid-option="tableGridOption"
          :table-grid-data-source-option="tableGridDataSourceOption"
          @persistSelection="selectDataItems"
        />
      </el-main>
    </el-container>
  </div>

</template>

<script>
  import kendoForm from '@/Views/components/kendo-vue-component/kendoForm/index.vue'
  // import { isBlank } from '@/Utils/strUtil.js'
  export default {
    name: 'PrjMemberAntSeltManage',
    components: {
      kendoForm
    },
    data() {
      return {
        /* Form*/
        kendoFormItems: [
          {
            name: 'atdMonth',
            label: '考勤结算月份',
            paramName: 'atdMonth',
            controlType: 'datePicker',
            format: 'yyyy-MM-dd',
            depth: 'year',
            required: true
          }
        ],
        kendoForm: {
          ref: 'prjMemberAtdSeltForm',
          valid: 'valid',
          buttons: [
            {
              name: '获取考勤结算', // 按钮内容
              class: 'k-primary', // 按钮样式
              icon: 'align-bottom-element',
              click: this.generate
            }
          ]
        },
        kendoParamsValue: {
          atdMonth: ''
        },
        tableGridOption: {
          ref: 'PrjMemberOptGrid',
          dataSourceRef: 'prjMemberDataSource',
          remoteData: true,
          sortable: true,
          reorderable: true,
          resizable: true,
          selectSingle: true,
          selectMultiple: false,
          toolbar: ['excel'],
          excel: {
            fileName: '月考勤结算表',
            allPages: true,
            filterable: true
          },
          columns: [
            {
              field: 'atdMonth',
              title: '考勤月份'
            },
            {
              field: 'memberNo',
              title: '项目成员',
              width: 120,
              dictData: 'MEMBER_NO_CD'
            },
            {
              field: 'shbAtdDays',
              title: '应出勤天数'
            },
            {
              field: 'actualAtdDays',
              title: '实际出勤天数'
            },
            {
              field: 'probationPeriod',
              title: '试用期天数'
            },
            {
              field: 'actualSeltDays',
              title: '实际结算天数'
            },
            {
              field: 'seltUnitPrice',
              title: '结算单价',
              format: '{0:c2}'
            },
            {
              field: 'actualSeltTotalPrice',
              title: '结算总价',
              format: '{0:c2}',
              aggregates: ['sum'],
              // eslint-disable-next-line no-undef
              groupFooterTemplate: '月小计：#=sum#'
            }
          ]
        },
        tableGridDataSourceOption: {
          ref: 'dictDataSource',
          type: 'json',
          transportReadUrl: '/service/prjMng/prjMemberMng/findPrjMemberAntSelt',
          remoteTransData: {
            MEMBER_NO_CD: {
              transportName: 'prjMemberMngServiceImpl',
              transportMethod: 'prjMemberTransDict',
              transportParam: {}
            },
            PRJ_NO_CD: {
              transportName: 'PrjInfoMngServiceImpl',
              transportMethod: 'prjTransDict',
              transportParam: {}
            }
          },
          params: {},
          group: {
            field: 'atdMonth',
            aggregates: [
              { field: 'actualSeltTotalPrice', aggregate: 'sum', format: '{0:c2}', type: 'number' }
            ]
          },
          schema: {
            model: {
              id: 'id',
              fields: {
                atdMonth: { editable: false, type: 'string' },
                memberNo: { editable: false, type: 'string' },
                shbAtdDays: { editable: false, type: 'string' },
                actualAtdDays: { editable: false, type: 'string' },
                probationPeriod: { editable: false, type: 'string' },
                actualSeltDays: { editable: false, type: 'string' },
                seltUnitPrice: { editable: false, type: 'number', format: '{0:c2}' },
                actualSeltTotalPrice: { editable: false, type: 'number', format: '{0:c2}' }
              }
            },
            data: function(response) {
              if (typeof response.data === 'object') {
                if (JSON.stringify(response.data) === '{}') {
                  return []
                }
              }
              return response.data
            }
          }
        },
        selectData: []
      }
    },
    mounted() {

    },
    methods: {
      /* grid*/
      parameterMap(options, operation) {
        if (operation !== 'read' && options.models) {
          // eslint-disable-next-line no-undef
          return { models: kendo.stringify(options.models) }
        } else {
          let parameterMap = {}
          parameterMap.pageNum = options.page
          parameterMap.pageSize = options.pageSize
          parameterMap = Object.assign(parameterMap, this.tableGridDataSourceOption.params)
          parameterMap = Object.assign(parameterMap, this.deepCopy(this.kendoParamsValue))
          return {
            // eslint-disable-next-line no-undef
            parameterMap: kendo.stringify(parameterMap)
          }
        }
      },
      selectDataItems(res) {
        this.selectData = res
      },
      generate(paramVal, validator) {
        if (paramVal.atdMonth) {
          const atdMonth = paramVal.atdMonth
          this.$post('/service/prjMng/prjMemberMng/hasPrjMemberAntSelt', ... paramVal).then(res => {
            if (res && res.data) {
              if (res.code !== 200) {
                this.$KendoNotify({
                  title: '错误信息:' + res.code,
                  message: res.message
                }, 'error')
              } else {
                this.$kendoConfirm({
                  title: '提示消息',
                  message: '是否需要重新生成考勤结算？',
                  btn: [
                    {
                      type: 'submit',
                      text: '确定'
                    },
                    {
                      type: 'cancel',
                      text: '取消'
                    }
                  ]
                }).then(_ => {
                  this.$post('/service/prjMng/prjMemberMng/generatePrjMemberAntSelt', { atdMonth: atdMonth }).then(res => {
                    if (res && res.data) {
                      if (res.code !== 200) {
                        this.$KendoNotify({
                          title: '错误信息:' + res.code,
                          message: res.message
                        }, 'error')
                      } else {
                        this.$set(paramVal, 'atdMonth', atdMonth)
                        this.tableGridDataSourceOption.params = Object.assign(this.tableGridDataSourceOption.params, paramVal)
                        this.$refs['PrjMemberAtdSeltKendoGrid'].transportReadGrid()
                      }
                    }
                  })
                }).catch(_ => {
                  this.tableGridDataSourceOption.params = {}
                  this.$refs['PrjMemberAtdSeltKendoGrid'].transportReadGrid()
                })
              }
            } else {
              this.$kendoConfirm({
                title: '提示消息',
                message: '当前月份无考勤结算记录，是否生成记录？',
                btn: [
                  {
                    type: 'submit',
                    text: '确定'
                  },
                  {
                    type: 'cancel',
                    text: '取消'
                  }
                ]
              }).then(_ => {
                this.$post('/service/prjMng/prjMemberMng/generatePrjMemberAntSelt', { atdMonth: atdMonth }).then(res => {
                  if (res && res.data) {
                    if (res.code !== 200) {
                      this.$KendoNotify({
                        title: '错误信息:' + res.code,
                        message: res.message
                      }, 'error')
                    } else {
                      this.$set(paramVal, 'atdMonth', atdMonth)
                      this.tableGridDataSourceOption.params = Object.assign(this.tableGridDataSourceOption.params, paramVal)
                      this.$refs['PrjMemberAtdSeltKendoGrid'].transportReadGrid()
                    }
                  }
                })
              }).catch(_ => {
                // 执行cancel方法
                this.$set(paramVal, 'atdMonth', atdMonth)
                this.tableGridDataSourceOption.params = Object.assign(this.tableGridDataSourceOption.params, paramVal)
                this.$refs['PrjMemberAtdSeltKendoGrid'].transportReadGrid()
              })
            }
          })
        } else {
          this.$KendoNotify({
            title: '提示信息:',
            message: '请选择生成考勤结算月份!'
          }, 'warning')
        }
      }
    }
  }
</script>

<style>

</style>
